package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Product;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.PageVo;
import com.insurance.domain.vo.ProductPageVo;
import com.insurance.mapper.ProductMapper;
import com.insurance.service.ProductService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2023-04-22 10:37:42
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public ResponseResult listByPage(ProductPageVo productPageVo) {
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(productPageVo.getName()),Product::getName,productPageVo.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(productPageVo.getCategory()),Product::getCategory,productPageVo.getCategory());
        queryWrapper.like(!ObjectUtils.isEmpty(productPageVo.getCompany()), Product::getCompany,productPageVo.getCompany());
        Page<Product> page = new Page<>(productPageVo.getPageNo(), productPageVo.getPageSize());
        page(page,queryWrapper);
        List<Product> products = page.getRecords();
        PageVo pageVo = new PageVo(products, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addProduct(Product product) {
        if(save(product)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAILED);
    }

    @Override
    public ResponseResult updateProduct(Product product) {
        if(updateById(product)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
